import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Register from '../components/Register.vue'
import Home from '../components/Home.vue'
import Attendance from '../components/Attendance.vue'
import Finance from '../components/Finance.vue'
import Notice from '../components/Notice.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/Register',
    name: 'Register',
    component: Register,
  	meta: { requiresAuth: true } 
  },
  {
    path: '/Home',
    name: 'Home',
    component: Home,
	meta: { requiresAuth: true } 
  },
  {
    path: '/attendance',
    name: 'Attendance',
    component: Attendance,
  },
  {
    path: '/Notice',
    name: 'Notice',
    component: Notice,
  },
  {
    path: '/Finance',
    name: 'Finance',
    component: Finance,
  },
  {
    path: '*',
    redirect: '/'  // 默认重定向到登录页
  }
]

const router = new VueRouter({
  mode: 'history',
  routes
})

// 添加路由守卫验证登录状态
router.beforeEach((to, from, next) => {
  const isLoggedIn = localStorage.getItem('token');
  if (to.meta.requiresAuth && !isLoggedIn) {
    next('/');
  } else {
    next();
  }
});
export default router